//
//  HOTMemoRangeViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-22.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTMemoCateViewController.h"
#import "HOTMemoRangeCell.h"

@interface HOTMemoCateViewController () <HOTMemoRangeCellDelegate> {
    
    NSArray *_dataList;
}

@end

@implementation HOTMemoCateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"カテゴリ"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_memo"]];
    
    _dataList = func_arrayFromPlist(@"notes_cate");
    
    //_dataList = @[@"未分類",@"お気に入り",@"気になる",@"腕がいい",@"金い悪い",@"腕が悪い/使えない",@"プラックリスト"];
}

#pragma mark - UItableView DataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellId = @"cellID";
    HOTMemoRangeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        
        UINib *nib = [UINib nibWithNibName:XIB(@"HOTMemoRangeCell") bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellId];
        
        cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    }
    
    cell.delegate = self;
    cell.indexPath = indexPath;
    NSDictionary *dic = [_dataList objectAtIndex:indexPath.row];
    cell.cateTitleLabel.text = [dic objectForKey:@"name"];
    
    if (indexPath.row == _dataList.count-1) {
        
        cell.cateTitleLabel.textColor = [UIColor colorWithRed:0.945 green:0.000 blue:0.000 alpha:1.000];
        [cell setBgColor:[UIColor colorWithWhite:0.059 alpha:1.000]];
    } else {
        cell.cateTitleLabel.textColor = [UIColor blackColor];
        [cell setBgColor:[UIColor colorWithWhite:0.892 alpha:1.000]];
    }
    
    if ([[dic objectForKey:@"id"] isEqualToString:self.selCateId]) {
        
        cell.radioBtn.selected = YES;
    } else {
        cell.radioBtn.selected = NO;
    }
    
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataList.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 62;
}

#pragma mark - UITableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.delegate respondsToSelector:@selector(selectedCate:name:)]) {
        
        NSDictionary *dic = [_dataList objectAtIndex:indexPath.row];
        [self.delegate selectedCate:[dic objectForKey:@"id"] name:[dic objectForKey:@"name"]];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)radioBtnClicked:(BOOL)isChecked index:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(selectedCate:name:)]) {
        
        NSDictionary *dic = [_dataList objectAtIndex:indexPath.row];
        [self.delegate selectedCate:[dic objectForKey:@"id"] name:[dic objectForKey:@"name"]];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
